package shuashuami.hb.com.http;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import shuashuami.hb.com.entity.agent.ApplyMsgBean;
import shuashuami.hb.com.entity.agent.ClickerBean;
import shuashuami.hb.com.entity.agent.DistributeValue;
import shuashuami.hb.com.entity.agent.PagerBean;
import shuashuami.hb.com.entity.agent.User;
import shuashuami.hb.com.manager.agent.UserManager;

/**
 * created by zhangqi at 2017/7/5
 */
public class HttpAgentMethods {

    private static Gson gson=null;


    static {
        gson=new Gson();
    }

    public static void getUserInfo(final HttpOnNextListener<User> listener, String agentId){
        Map<String, String> map = new HashMap<>();
        map.put("agentId", agentId + "");
        HttpUtils.doPost(URLs.agentUserInfo, map, new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("zq","aaa");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    if (response.code() == 200) {
                        Type jsonType = new TypeToken<HttpResult<User>>() {
                        }.getType();

                        String str=response.body().string();

                        try {
                            JSONObject object=new JSONObject(str);
                            int statu=object.getInt("status");
                            if(statu==200){
                                User user=UserManager.getInstance().getUser();
                                user.resolveByJson(object.getString("content"));
                                listener.onNext(user);
                            }else{
                                listener.onNext(UserManager.getInstance().getUser());
                            }
                        }catch (Exception e){

                        }

                    }
                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });
    }

    public static void changeMessage(final HttpOnNextListener listener,String agentId,String type,String date){
        Map<String, String> map = new HashMap<>();
        map.put("agentId", agentId + "");
        map.put("type",type);
        map.put("data",date);

        HttpUtils.doPost(URLs.agentUserEdit, map, new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("zq","aaa");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    if (response.code() == 200) {
                        Type jsonType = new TypeToken<HttpResult<String>>() {
                        }.getType();

                        if(listener!=null)
                            listener.onNext(response.body().string());
                    }
                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });
    }

    public static void getMyClicker(final HttpOnNextListener<String> listener, int type, int level, int page){
        Map<String, String> map = new HashMap<>();
        map.put("agentId", UserManager.getInstance().getUser().getId() + "");
        map.put("type",type+"");
        map.put("level",level+"");
        map.put("page",page+"");
        map.put("size","30");

        HttpUtils.doPost(URLs.agentGetClicker, map, new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("zq","aaa");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    if (response.code() == 200) {
                        Type jsonType = new TypeToken<HttpResult<PagerBean<ClickerBean>>>() {
                        }.getType();

                        String str=response.body().string();


                        if(listener!=null)
                            listener.onNext(str);
                    }

                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });
    }


    public static void getApplyInfo(final HttpOnNextListener<String > listener, String id) {

        Map<String, String> map = new HashMap<>();
        map.put("agentId", UserManager.getInstance().getUser().getId() + "");
        map.put("brush_id",id);

        HttpUtils.doPost(URLs.viewApply, map, new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("zq","aaa");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    if (response.code() == 200) {
                        Type jsonType = new TypeToken<HttpResult<ApplyMsgBean>>() {
                        }.getType();

                    //    HttpResult<ApplyMsgBean> httpResult = gson.fromJson(response.body().string(), jsonType);



                        if(listener!=null)
                            listener.onNext(response.body().string());
                    }

                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });

    }

    public static void detalApplyInfo(final HttpOnNextListener<String> listener, String brush_id, int i) {
        Map<String, String> map = new HashMap<>();
        map.put("agentId", UserManager.getInstance().getUser().getId() + "");
        map.put("brush_id",brush_id);
        map.put("status",i+"");

        HttpUtils.doPost(URLs.detalApply, map, new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("zq","aaa");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    if (response.code() == 200) {
                        Type jsonType = new TypeToken<HttpResult<String>>() {
                        }.getType();

//                        HttpResult<String> httpResult = gson.fromJson(response.body().string(), jsonType);


                        if(listener!=null)
                            listener.onNext(response.body().string());
                    }
                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });

    }

    public static void generateCode(final HttpOnNextListener<List<String>> listener) {

        Map<String, String> map = new HashMap<>();
        map.put("agentId", UserManager.getInstance().getUser().getId() + "");


        HttpUtils.doPost(URLs.generateCode, map, new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("zq","aaa");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    if (response.code() == 200) {
                        Type jsonType = new TypeToken<HttpResult<List<String>>>() {
                        }.getType();

                        HttpResult<List<String>> httpResult = gson.fromJson(response.body().string(), jsonType);


                        if(listener!=null)
                            listener.onNext((List<String>) httpResult.getContent());
                    }

                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });
    }

    public static void getAgentOrders(final HttpOnNextListener listener, int disType, int page) {

        Map<String, String> map = new HashMap<>();
        map.put("agentId", UserManager.getInstance().getUser().getId() + "");
        map.put("type",disType+"");
        map.put("page",page+"");
        map.put("size","30");

        HttpUtils.doPost(URLs.getDistributeOrder, map, new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("zq","aaa");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    if (response.code() == 200) {
//                        Type jsonType = new TypeToken<HttpResult<PagerBean<OrderBean>>>() {
//                        }.getType();

                   //     HttpResult<PagerBean<OrderBean>> httpResult = gson.fromJson(response.body().string(), jsonType);

                        if(listener!=null)
                            listener.onNext(response.body().string());
                    }

                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });
    }

    public static void getDistributeBaseData(final HttpOnNextListener<DistributeValue> listener) {
        Map<String, String> map = new HashMap<>();
        map.put("agentId", UserManager.getInstance().getUser().getId() + "");

        HttpUtils.doPost(URLs.getDistributeValue, map, new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("zq","aaa");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    if (response.code() == 200) {
                        Type jsonType1=new TypeToken<HttpResult<DistributeValue>>(){}.getType();

//
//                        Type jsonType = new TypeToken<HttpResult<String>>() {
//                        }.getType();


                        HttpResult<DistributeValue> httpResult = gson.fromJson(response.body().string(), jsonType1);



                        if(listener!=null)
                            listener.onNext((DistributeValue) httpResult.getContent());
                    }

                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });

    }

    //刷手w我的刷手
    public static void recommendListc(final HttpOnNextListener listener, String page, String size) {
        Map<String, String> map = new HashMap<>();
        map.put("agentId", UserManager.getInstance().getUser().getId());
        map.put("page", page);
        map.put("size", size);

        HttpUtils.doPost(URLs.recommendListc, map, new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    listener.onNext(response.body().string());
                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });
    }

    public static void getYuE(final HttpOnNextListener<String> listener) {

        Map<String, String> map = new HashMap<>();
        map.put("agentId", UserManager.getInstance().getUser().getId());

        HttpUtils.doPost(URLs.getYuE, map, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    listener.onNext(response.body().string());
                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });
    }

    public static void getZhifubao(final HttpOnNextListener<String> listener) {

        Map<String, String> map = new HashMap<>();
        map.put("agentId", UserManager.getInstance().getUser().getId());

        HttpUtils.doPost(URLs.getZhifubao, map, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    listener.onNext(response.body().string());
                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });
    }

    public static void getBank(final HttpOnNextListener<String> listener) {
        Map<String, String> map = new HashMap<>();
        map.put("agentId", UserManager.getInstance().getUser().getId());

        HttpUtils.doPost(URLs.getYInhangkaInfo, map, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    listener.onNext(response.body().string());
                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });
    }


    public static void tixian(final HttpOnNextListener<String> listener, String s, String type) {

        Map<String, String> map = new HashMap<>();
        map.put("agentId", UserManager.getInstance().getUser().getId());
        map.put("money", s);
        map.put("type", type);

        HttpUtils.doPost(URLs.tixian, map, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    listener.onNext(response.body().string());
                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });
    }

    public static void saveZhifubao(final HttpOnNextListener<String> listener, String name, String num) {
        Map<String, String> map = new HashMap<>();
        map.put("agentId", UserManager.getInstance().getUser().getId());
        map.put("alipay_realname", name);
        map.put("alipay_account", num);

        HttpUtils.doPost(URLs.saveZhifubao, map, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    listener.onNext(response.body().string());
                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });
    }

    public static void saveYinhangka(final HttpOnNextListener listener,  String bankcard_name, String bankcard_identify
            , String bankcard_no, String bank_id, String bankcard_address, String bankcard_info) {
        Map<String, String> map = new HashMap<>();
        map.put("agentId", UserManager.getInstance().getUser().getId());
        map.put("bankcard_name", bankcard_name);
        map.put("bankcard_identify", bankcard_identify);
        map.put("bankcard_no", bankcard_no);
        map.put("bank_id", bank_id);
        map.put("bankcard_address", bankcard_address);
        map.put("bankcard_info", bankcard_info);

        HttpUtils.doPost(URLs.saveYInhangka, map, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    listener.onNext(response.body().string());
                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });

    }

    public static void getYongJinmingxi(final HttpOnNextListener<String> listener, String id,String page, String type, String size) {
        Map<String, String> map = new HashMap<>();
        map.put("agentId", id);
        map.put("page", page);
        map.put("type", type);
        map.put("size", size);

        HttpUtils.doPost(URLs.ayongjinmingxi, map, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    listener.onNext(response.body().string());
                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });

    }

    public static void getJilu(final HttpOnNextListener listener,String agentId, String page, String size) {
        Map<String, String> map = new HashMap<>();
        map.put("agentId", agentId);
        map.put("page", page);
        map.put("size", size);

        HttpUtils.doPost(URLs.jilu, map, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    listener.onNext(response.body().string());
                }
                //关闭防止内存泄漏
                if (response.body() != null) {
                    response.body().close();
                }
            }
        });
    }
}
